package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "地址簿相关接口")
@RequestMapping("/user/addressBook")
@RestController
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook 地址数据
     */
    @ApiOperation(value = "新增地址")
    @PostMapping
    public Result<String> addAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 删除地址
     * @param id 地址id
     */
    @ApiOperation(value = "删除地址")
    @ApiImplicitParam(name = "id", value = "地址id", required = true, paramType = "query", dataType = "long")
    @DeleteMapping
    public Result<String> deleteAddressBook(@RequestParam Long id) {
        addressBookService.deleteAddressBook(id);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id 地址id
     * @return 地址数据
     */
    @ApiOperation(value = "根据id查询地址")
    @ApiImplicitParam(name = "id", value = "地址id", required = true, paramType = "path", dataType = "long")
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBook(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getAddressBook(id);
        return Result.success(addressBook);
    }

    /**
     * 更新地址
     * @param addressBook 地址数据
     */
    @ApiOperation(value = "更新地址")
    @PutMapping
    public Result<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 更新默认地址
     * @param addressBook 地址数据
     */
    @ApiOperation(value = "更新默认地址")
    @PutMapping("/default")
    @Transactional
    public Result<String> updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateDefaultAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 查询默认地址
     */
    @ApiOperation(value = "查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddressBook() {
        List<AddressBook> addressBookList = addressBookService.getDefaultAddressBook();
        if(addressBookList != null && !addressBookList.isEmpty()) {
            return Result.success(addressBookList.get(0));
        }
        return Result.error("该用户没有默认地址");
    }

    /**
     * 查询地址列表
     */
    @ApiOperation(value = "查询地址列表")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAddressBookList() {
        List<AddressBook> addressBookList = addressBookService.getAddressBookList();
        return Result.success(addressBookList);
    }

}
